
var moodHistory = new observableArray();
var moodList = new observableArray();
for (var i = 0; i < 7; i++) {
    moodList.push({ mood: "" });
};
var timeFactory = require("~/user-module/timeFactory.js").timeFactory;
var timeFactory = new timeFactory();
// var Mood_server_url = "http://192.168.31.175:1314/api/complex/MoodsFactory";
var Mood_server_url = "http://121.40.18.23:1314/api/complex/MoodsFactory";

// for (var i = 6; i > 0; i--) {
//     var day = new Date(mTime - (i * 24 * 60 * 60 * 1000));
//     moodHistory.push({ dayofmonth: day.getDate(), dayofweek: timeFactory.getWeekDayEn(day.getDay(), true) });
// };

// moodHistory.push({ dayofmonth: today.getDate(), dayofweek: timeFactory.getWeekDayEn(today.getDay(), true) });

var isTodayMoodSubmit = new observable({ status: false });

var moodPage;
exports.moodloaded = args => {
    moodPage = args.object;
    moodPage.bindingContext = new observable({
        moodHistory: moodHistory,
        moodList: moodList,
        MoodHeadViewLeft: DeviceInfo.widthDIPs / 2 - 50,
        icon: iconPackage,
        isTodayMoodSubmit: isTodayMoodSubmit
    });
    getMoodList();
};


var new_mood;
var todayMood;
exports.postMood = params => {
    if (!new_mood) {
        dialogs.alert("Please Choose a Mood First!!").then(function() {
            console.log("Please Choose a Mood First!!");
        });
        return;
    };
    dialogs.confirm("Update Today's Mood???").then(function(result) {
        if (!result) {
            return;
        };
        isTodayMoodSubmit.status ? updateMood(todayMood, new_mood) : createMood(new_mood);
        new_mood = "";
    });
};

var _todayMoodIndex = 0;
var getMoodList = params => {
    http.getJSON({
        url: Mood_server_url,
        method: "GET",
        headers: { "Content-Type": "application/json" },
    }).then(function(responseList) {
        // console.log(JSON.stringify(responseList));
        var len = responseList.length;
        // console.log(len)
        if (len) {
            var lastMood = responseList[len - 1];
            _todayMoodIndex = len;
            var date = timeFactory.getDate("YYYY-MM-DD");
            (date == timeFactory.getDate("YYYY-MM-DD", lastMood.time)) ? (isTodayMoodSubmit.status = true, _todayMoodIndex-- , todayMood = lastMood._id) : (isTodayMoodSubmit.status = false);
            for (var i = 0; i < len; i++) {
                var date = new Date(responseList[i].time);
                moodHistory.push({
                    dayofmonth: date.getDate(),
                    dayofweek: timeFactory.getWeekDayEn(date.getDay(), true)
                });
                moodList.setItem(i, {
                    mood: String.fromCharCode(responseList[i].Mood)
                });
            };
            if (len < 7) {
                var date = new Date().getTime();
                isTodayMoodSubmit.status ? (date = date + 24 * 60 * 60 * 1000) : date;
                for (var i = 0; i < (7 - len); i++) {
                    var _d = new Date(date + (i * 24 * 60 * 60 * 1000));
                    moodHistory.push({
                        dayofmonth: _d.getDate(),
                        dayofweek: timeFactory.getWeekDayEn(_d.getDay(), true)
                    });
                }
            } else {
                _todayMoodIndex = 6;
                if (!isTodayMoodSubmit.status) {
                    var date = new Date();
                    moodHistory.shift();
                    moodList.setItem(6, { mood: "" });
                    moodHistory.push({
                        dayofmonth: date.getDate(),
                        dayofweek: timeFactory.getWeekDayEn(date.getDay(), true)
                    });
                };
            }
        } else {
            for (var i = 0; i < 7; i++) {
                var today = new Date();
                var mTime = today.getTime();
                var day = new Date(mTime + (i * 24 * 60 * 60 * 1000));
                moodHistory.push({
                    dayofmonth: day.getDate(),
                    dayofweek: timeFactory.getWeekDayEn(day.getDay(), true)
                });
            };
        };

    });
};

var createMood = params => {
    http.request({
        url: Mood_server_url,
        method: "POST",
        headers: { "Content-Type": "application/json" },
        content: JSON.stringify({ Mood: params })
    }).then(function(response) {
        dialogs.alert("Create Mood Sucess!!!").then(() => {
            console.log("mood post!!!");
        });
        getMoodList();
    });
    // console.log(JSON.stringify(response));
};

var updateMood = (params1, params2) => {
    console.log(params1);
    http.request({
        url: Mood_server_url,
        method: "PUT",
        headers: { "Content-Type": "application/json" },
        content: JSON.stringify({ _id: params1, mood: params2 })
    }).then(function(response) {
        getMoodList();
    });
};

// label tap to get what mood be choosen
exports.moodlabeltap = args => {
    new_mood = "0x" + args.view.text.charCodeAt(0).toString(16);
    moodList.setItem(_todayMoodIndex, { mood: String.fromCharCode(new_mood) });
    // console.log(moodList.toString())
    // console.log(new_mood);
};
